<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #dept_id="{ record }">
        <a-tag color="blue">{{ record?.dept?.name }}</a-tag>
      </template>
      
    </ma-crud>
    <div ref="containers" id="containers" class="container" style="width:100%;height:500px;position: absolute;top:-99999999999999999px"></div>
  </div>
</template>


<script setup>
import { ref, reactive,onMounted, onUnmounted } from 'vue'
import predictionAddress from '@/api/prediction/predictionAddress'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'



let map = null
const state = reactive({
  markerLayer: '',
  marker: null,
  infoWindow: null,
  value: 1,
  adders:'',
  lat:'',
  lng:'',
  geocoder:null
})
const TMap = (window).TMap

const crudRef = ref()
const dept_id = ref()
const inforow = ref()
const mapvisible = ref(false)
const containers = ref()


const open = (row) => {
  dept_id.value = row.depts[0].id
  inforow.value = row
  crudRef.value.requestData()
}

const setYph = ()=>{
  mapvisible.value = true
  init()
}

const init = () => {
  const center = new TMap.LatLng(39.910506, 116.39926431433355)
  map = new TMap.Map(containers.value, {
    center: center, // 设置地图中心点坐标
    zoom: 11, // 设置地图缩放级别
    viewMode: '2D',
  })
  // 监听地图点击事件
  map.on('click', (e) => {
    // 清除点标记
    state.marker.setMap(null)
    // 关闭信息窗关闭
    state.infoWindow.close()
    state.marker = null
  })
 
  // 初始化infoWindow
  state.infoWindow = new TMap.InfoWindow({
    map: map,
    position: center,
    offset: { x: 0, y: -32 }, // 设置信息窗相对position偏移像素
  })
  // 初始关闭信息窗关闭
  state.infoWindow.close()
}

 



const numberOperation = (newValue, id, numberName) => {
  predictionAddress.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  predictionAddress.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'flyos_prediction_address',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: predictionAddress.getList,
  recycleApi: predictionAddress.getRecycleList,
  add: {
    show: true,
    api: predictionAddress.save,
    auth: ['prediction:address:save']
  },
  edit: {
    show: true,
    api: predictionAddress.update,
    auth: ['prediction:address:update']
  },
  delete: {
    show: true,
    api: predictionAddress.deletes,
    auth: ['prediction:address:delete'],
    realApi: predictionAddress.realDeletes,
    realAuth: ['prediction:address:realDeletes']
  },
  recovery: {
    show: true,
    api: predictionAddress.recoverys,
    auth: ['prediction:address:recovery']
  },
  import: {
    show: true,
    url: 'prediction/address/import',
    templateUrl: 'prediction/address/downloadTemplate',
    auth: ['prediction:address:import']
  },
  export: {
    show: true,
    url: 'prediction/address/export',
    auth: ['prediction:address:export']
  },
  beforeRequest: (params) => {
      params.dept_id = dept_id.value
      params.order = 'created_at'
      params.orderType = 'desc'
  }
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: '公司名称',
    dataIndex: 'dept_id',
    align:'center',
    formType: 'select',
    disabled: true,
    addDefaultValue: async function () {
      return dept_id;
    },
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'system/dept/remote',
        // 指定组件接收的props
        props: { label: 'name', value: 'id' },
        // 开启分页
        openPage: false,
        // 远程请求配置项
        remoteOption: {
            // 按用户名排序
            sort: { id: 'desc' }, // 如果不指定排序方式，默认为正序排序
            // 设置查询的字段
            select: [ 'id', 'name' ],
            // 设置数据过滤
            filter: {
              type:['=',2],
              status: ['=',1]
            },
            // 关联模型
            relations: [
            ]
        }
    }
  },
  {
    title: "手机号码",
    dataIndex: "mobile",
    formType: "input",
    search: true
  },
  {
    title: "地址",
    dataIndex: "address",
    formType: "input-search",
    search: true,
    searchButton:true,
    buttonText:'获取经纬度',
    onInputSearch:(e)=>{
      // console.log('点击搜索',e);
      // console.log(crudRef.value.crudFormRef.form.lng)
      // console.log(columns)
      // crudRef.value.crudFormRef.form.lng = 105.569481
      const geocoder = new TMap.service.Geocoder() // 新建一个正逆地址解析类
      geocoder.getLocation({address:e}).then((res) => {
        if (res.status === 0) {
          crudRef.value.crudFormRef.form.lat = res.result.location.lat
          crudRef.value.crudFormRef.form.lng = res.result.location.lng
          
        }
      }).catch(rej=>{
        Message.error('地址解析错误')
      })
    },
  },
  {
    title: "座机号码",
    dataIndex: "tel",
    formType: "input",
    search: true
  },
  {
    title: "单位",
    dataIndex: "unit",
    formType: "input",
    search: true
  },
  {
    title: "纬度",
    dataIndex: "lat",
    formType: "input",
    addDisabled:true,
    search: true
  },
  {
    title: "经度",
    dataIndex: "lng",
    formType: "input",
    addDisabled:true,
    search: true,
  },
  {
    title: "是否默认",
    dataIndex: "is_default",
    formType: "select",
    search: true,
    dict: {
      data: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      translation: true
    }
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    search: true,
    addDisplay: false,
    editDisplay: false,
    sortable: {
      sortDirections: [
        "ascend",
        "descend"
      ],
      sorter: true
    },
    showTime: true
  }
])
defineExpose({ open })
</script>
<script> export default { name: 'prediction:address' } </script>